/**
 * 
 */
package cn.com.gzheroli.herojdbc;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.gzheroli.core.util.NameUtils;

/**
 * @类名: JdbcUtil
 * @描述: TODO(这里用一句话描述这个类的作用)
 * @作者: 李长荣 lichang_20011@163.com
 * @日期: 2017年10月19日 上午8:13:20
 * @version V1.0
 * 
 */
public class JdbcUtil {
	private static final Log log = LogFactory.getLog(JdbcUtil.class);
	private static final long serialVersionUID = 0xb61cc89c7349f836L;
	private Connection conn;
	private Statement stmt;
	private String sql;
	private ResultSet rs;

	/**
	 * @throws SQLException
	 * 			@Title: main @Description: TODO(这里用一句话描述这个方法的作用) @param @param
	 *             args 设定文件 @return void 返回类型 @throws
	 */
	public static void main(String[] args) throws SQLException {

	}

	public String getTableComment(String tableName) {
		String tableComment = "";
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			this.conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			this.stmt = this.conn.createStatement(1005, 1007);

			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				this.sql = "select TABLE_COMMENT from information_schema.TABLES where TABLE_NAME='" + tableName + "'";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				this.sql = " select distinct colstable.table_name as  table_name from user_tab_cols colstable";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				this.sql = "SELECT distinct c.relname AS  table_name FROM pg_class c";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				this.sql = "select distinct c.name as  table_name from sys.objects c ";
			}

			this.rs = this.stmt.executeQuery(this.sql);
			while (this.rs.next()) {
				return this.rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			closeConnection();
		} finally {
			closeConnection();
		}
		return tableName;
	}

	public void closeConnection() {
		try {
			if (this.stmt != null) {
				this.stmt.close();
				this.stmt = null;
				System.gc();
			}
			if (this.conn != null) {
				this.conn.close();
				this.conn = null;
				System.gc();
			}
		} catch (SQLException e) {
			try {
				throw e;
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}

	public List<String> readAllTableNames() throws SQLException {
		List<String> tableNames = new ArrayList<String>(0);
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			this.conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			this.stmt = this.conn.createStatement(1005, 1007);

			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				this.sql = MessageFormat.format(
						"select distinct table_name from information_schema.columns where table_schema = {0}",
						new Object[] { TableConvert.getV(CodeResourceUtil.DATABASE_NAME) });
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				this.sql = " select distinct colstable.table_name as  table_name from user_tab_cols colstable";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				this.sql = "SELECT distinct c.relname AS  table_name FROM pg_class c";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				this.sql = "select distinct c.name as  table_name from sys.objects c ";
			}

			this.rs = this.stmt.executeQuery(this.sql);
			while (this.rs.next()) {
				String tableName = this.rs.getString(1);
				tableNames.add(tableName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			closeConnection();
		} finally {
			closeConnection();
		}
		return tableNames;
	}

	public List readTableColumn(String tableName) throws Exception {
		List columntList = new ArrayList();
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			stmt = conn.createStatement(1005, 1007);
			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				sql = MessageFormat.format(
						"select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()),
								TableConvert.getV(CodeResourceUtil.DATABASE_NAME) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				sql = MessageFormat.format(
						" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = {0}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				sql = MessageFormat.format(
						"SELECT a.attname AS  field,t.typname AS type,col_description(a.attrelid,a.attnum) as comment,null as column_precision,null as column_scale,null as Char_Length,a.attnotnull  FROM pg_class c,pg_attribute  a,pg_type t  WHERE c.relname = {0} and a.attnum > 0  and a.attrelid = c.oid and a.atttypid = t.oid  ORDER BY a.attnum ",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				sql = MessageFormat.format(
						"select cast(a.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as varchar(200)) comment,  cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale,  a.max_length,  (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable   from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.objects c on a.object_id=c.object_id and c.type='''U''' left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0}",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			rs = stmt.executeQuery(sql);
			rs.last();
			int fieldNum = rs.getRow();
			int n = fieldNum;
			if (n > 0) {
				Columnt columnt = new Columnt();
				if (CodeResourceUtil.FILED_CONVERT) {
					columnt.setFieldName(formatField(rs.getString(1).toLowerCase()));
				} else {
					columnt.setFieldName(rs.getString(1).toLowerCase());
				}
				columnt.setFieldDbName(rs.getString(1).toUpperCase());
				columnt.setFieldType(formatField(rs.getString(2).toLowerCase()));
				columnt.setPrecision(rs.getString(4));
				columnt.setScale(rs.getString(5));
				columnt.setCharmaxLength(rs.getString(6));
				columnt.setNullable(TableConvert.getNullAble(rs.getString(7)));
				formatFieldClassType(columnt);
				columnt.setFiledComment(
						StringUtils.isBlank(rs.getString(3)) ? columnt.getFieldName() : rs.getString(3));
				/*
				 * String ui_filter_fields[] = new String[0]; if
				 * (CodeResourceUtil.GENERATE_UI_FILTER_FIELDS != null) ui_filter_fields =
				 * CodeResourceUtil.GENERATE_UI_FILTER_FIELDS.toLowerCase().split(","); if
				 * (!CodeResourceUtil.GENERATE_TABLE_ID.equals(columnt.getFieldName()) &&
				 * !CodeStringUtils.isIn(columnt.getFieldDbName().toLowerCase(),
				 * ui_filter_fields)) columntList.add(columnt);
				 */
				columntList.add(columnt);
				while (rs.previous()) {
					Columnt po = new Columnt();
					if (CodeResourceUtil.FILED_CONVERT) {
						po.setFieldName(formatField(rs.getString(1).toLowerCase()));
					} else {
						po.setFieldName(rs.getString(1).toLowerCase());
					}
					po.setFieldDbName(rs.getString(1).toUpperCase());
					// if (!CodeResourceUtil.GENERATE_TABLE_ID.equals(po.getFieldName())&&
					// !CodeStringUtils.isIn(po.getFieldDbName().toLowerCase(), ui_filter_fields)) {
					po.setFieldType(formatField(rs.getString(2).toLowerCase()));
					po.setPrecision(rs.getString(4));
					po.setScale(rs.getString(5));
					po.setCharmaxLength(rs.getString(6));
					po.setNullable(TableConvert.getNullAble(rs.getString(7)));
					formatFieldClassType(po);
					po.setFiledComment(StringUtils.isBlank(rs.getString(3)) ? po.getFieldName() : rs.getString(3));
					columntList.add(po);
					// }
				}
			} else {
				throw new Exception("该表不存在或者表中没有字段");
			}
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		} finally {
			closeConnection();
		}

		List rsList = new ArrayList();
		for (int i = columntList.size() - 1; i >= 0; i--) {
			Columnt ch = (Columnt) columntList.get(i);
			rsList.add(ch);
		}

		return rsList;
	}

	public List getTableColumn(String tableName) throws Exception {
		List columntList = new ArrayList();
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			stmt = conn.createStatement(1005, 1007);
			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				sql = MessageFormat.format(
						"select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()),
								TableConvert.getV(CodeResourceUtil.DATABASE_NAME) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				sql = MessageFormat.format(
						" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = {0}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				sql = MessageFormat.format(
						"SELECT a.attname AS  field,t.typname AS type,col_description(a.attrelid,a.attnum) as comment,null as column_precision,null as column_scale,null as Char_Length,a.attnotnull  FROM pg_class c,pg_attribute  a,pg_type t  WHERE c.relname = {0} and a.attnum > 0  and a.attrelid = c.oid and a.atttypid = t.oid  ORDER BY a.attnum ",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				sql = MessageFormat.format(
						"select cast(a.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as varchar(200)) comment,  cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale,  a.max_length,  (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable   from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.objects c on a.object_id=c.object_id and c.type='''U''' left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0}",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			rs = stmt.executeQuery(sql);
			rs.last();
			int fieldNum = rs.getRow();
			int n = fieldNum;
			if (n > 0) {
				Columnt columnt = new Columnt();
				if (CodeResourceUtil.FILED_CONVERT) {
					columnt.setFieldName(formatField(rs.getString(1).toLowerCase()));
				} else {
					columnt.setFieldName(rs.getString(1).toLowerCase());
				}
				columnt.setFieldDbName(rs.getString(1).toUpperCase());
				columnt.setFieldType(formatField(rs.getString(2).toLowerCase()));
				columnt.setPrecision(rs.getString(4));
				columnt.setScale(rs.getString(5));
				columnt.setCharmaxLength(rs.getString(6));
				columnt.setNullable(TableConvert.getNullAble(rs.getString(7)));
				formatFieldClassType(columnt);
				columnt.setFiledComment(
						StringUtils.isBlank(rs.getString(3)) ? columnt.getFieldName() : rs.getString(3));
				columntList.add(columnt);
				while (rs.previous()) {
					Columnt po = new Columnt();
					if (CodeResourceUtil.FILED_CONVERT) {
						po.setFieldName(formatField(rs.getString(1).toLowerCase()));
					} else {
						po.setFieldName(rs.getString(1).toLowerCase());
					}
					po.setFieldDbName(rs.getString(1).toUpperCase());
					po.setFieldType(formatField(rs.getString(2).toLowerCase()));
					po.setPrecision(rs.getString(4));
					po.setScale(rs.getString(5));
					po.setCharmaxLength(rs.getString(6));
					po.setNullable(TableConvert.getNullAble(rs.getString(7)));
					formatFieldClassType(po);
					po.setFiledComment(StringUtils.isBlank(rs.getString(3)) ? po.getFieldName() : rs.getString(3));
					columntList.add(po);
				}
			} else {
				throw new Exception("该表不存在或者表中没有字段");
			}
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		} finally {
			closeConnection();
		}

		List rsList = new ArrayList();
		for (int i = columntList.size() - 1; i >= 0; i--) {
			Columnt ch = (Columnt) columntList.get(i);
			rsList.add(ch);
		}

		return rsList;
	}

	public List readOriginalTableColumn(String tableName) throws Exception {
		List columntList = new ArrayList();
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			stmt = conn.createStatement(1005, 1007);
			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				sql = MessageFormat.format(
						"select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = {0} and table_schema = {1}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()),
								TableConvert.getV(CodeResourceUtil.DATABASE_NAME) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				sql = MessageFormat.format(
						" select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment, colstable.Data_Precision column_precision, colstable.Data_Scale column_scale,colstable.Char_Length,colstable.nullable from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = {0}",
						new Object[] { TableConvert.getV(tableName.toUpperCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				sql = MessageFormat.format(
						"SELECT a.attname AS  field,t.typname AS type,col_description(a.attrelid,a.attnum) as comment,null as column_precision,null as column_scale,null as Char_Length,a.attnotnull  FROM pg_class c,pg_attribute  a,pg_type t  WHERE c.relname = {0} and a.attnum > 0  and a.attrelid = c.oid and a.atttypid = t.oid  ORDER BY a.attnum ",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				sql = MessageFormat.format(
						"select cast(a.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as varchar(200)) comment,  cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale,  a.max_length,  (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable   from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.objects c on a.object_id=c.object_id and c.type='''U''' left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0}",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			rs = stmt.executeQuery(sql);
			rs.last();
			int fieldNum = rs.getRow();
			int n = fieldNum;
			if (n > 0) {
				Columnt columnt = new Columnt();
				if (CodeResourceUtil.FILED_CONVERT) {
					columnt.setFieldName(formatField(rs.getString(1).toLowerCase()));
				} else {
					columnt.setFieldName(rs.getString(1).toLowerCase());
				}
				columnt.setFieldDbName(rs.getString(1).toUpperCase());
				columnt.setPrecision(TableConvert.getNullString(rs.getString(4)));
				columnt.setScale(TableConvert.getNullString(rs.getString(5)));
				columnt.setCharmaxLength(TableConvert.getNullString(rs.getString(6)));
				columnt.setNullable(TableConvert.getNullAble(rs.getString(7)));
				columnt.setFieldType(
						formatDataType(rs.getString(2).toLowerCase(), columnt.getPrecision(), columnt.getScale()));
				formatFieldClassType(columnt);
				columnt.setFiledComment(
						StringUtils.isBlank(rs.getString(3)) ? columnt.getFieldName() : rs.getString(3));
				columntList.add(columnt);
				Columnt po;
				for (; rs.previous(); columntList.add(po)) {
					po = new Columnt();
					if (CodeResourceUtil.FILED_CONVERT) {
						po.setFieldName(formatField(rs.getString(1).toLowerCase()));
					} else {
						po.setFieldName(rs.getString(1).toLowerCase());
					}
					po.setFieldDbName(rs.getString(1).toUpperCase());
					po.setPrecision(TableConvert.getNullString(rs.getString(4)));
					po.setScale(TableConvert.getNullString(rs.getString(5)));
					po.setCharmaxLength(TableConvert.getNullString(rs.getString(6)));
					po.setNullable(TableConvert.getNullAble(rs.getString(7)));
					po.setFieldType(formatDataType(rs.getString(2).toLowerCase(), po.getPrecision(), po.getScale()));
					formatFieldClassType(po);
					po.setFiledComment(StringUtils.isBlank(rs.getString(3)) ? po.getFieldName() : rs.getString(3));
				}

			} else {
				throw new Exception("该表不存在或者表中没有字段");
			}
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		} finally {
			closeConnection();
		}

		List rsList = new ArrayList();
		for (int i = columntList.size() - 1; i >= 0; i--) {
			Columnt ch = (Columnt) columntList.get(i);
			rsList.add(ch);
		}

		return rsList;
	}

	public static String formatField(String field) {
		String strs[] = field.split("_");
		field = "";
		int m = 0;
		for (int length = strs.length; m < length; m++) {
			if (m > 0) {
				String tempStr = strs[m].toLowerCase();
				tempStr = (new StringBuilder(String.valueOf(tempStr.substring(0, 1).toUpperCase())))
						.append(tempStr.substring(1, tempStr.length())).toString();
				field = (new StringBuilder(String.valueOf(field))).append(tempStr).toString();
			} else {
				field = (new StringBuilder(String.valueOf(field))).append(strs[m].toLowerCase()).toString();
			}
		}
		return field;
	}

	public static String formatFieldCapital(String field) {
		String strs[] = field.split("_");
		field = "";
		int m = 0;
		for (int length = strs.length; m < length; m++) {
			if (m > 0) {
				String tempStr = strs[m].toLowerCase();
				tempStr = (new StringBuilder(String.valueOf(tempStr.substring(0, 1).toUpperCase())))
						.append(tempStr.substring(1, tempStr.length())).toString();
				field = (new StringBuilder(String.valueOf(field))).append(tempStr).toString();
			} else {
				field = (new StringBuilder(String.valueOf(field))).append(strs[m].toLowerCase()).toString();
			}
		}
		field = (new StringBuilder(String.valueOf(field.substring(0, 1).toUpperCase()))).append(field.substring(1))
				.toString();
		return field;
	}

	public boolean checkTableExist(String tableName) {
		try {
			System.out.println("数据库驱动: " + CodeResourceUtil.DIVER_NAME);
			Class.forName(CodeResourceUtil.DIVER_NAME);
			this.conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			this.stmt = this.conn.createStatement(1005, 1007);

			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				this.sql = ("select column_name,data_type,column_comment,0,0 from information_schema.columns where table_name = '"
						+ tableName.toUpperCase() + "'" + " and table_schema = '" + CodeResourceUtil.DATABASE_NAME
						+ "'");
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				this.sql = ("select colstable.column_name column_name, colstable.data_type data_type, commentstable.comments column_comment from user_tab_cols colstable  inner join user_col_comments commentstable  on colstable.column_name = commentstable.column_name  where colstable.table_name = commentstable.table_name  and colstable.table_name = '"
						+ tableName.toUpperCase() + "'");
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				this.sql = MessageFormat.format(
						"SELECT a.attname AS  field,t.typname AS type,col_description(a.attrelid,a.attnum) as comment,null as column_precision,null as column_scale,null as Char_Length,a.attnotnull  FROM pg_class c,pg_attribute  a,pg_type t  WHERE c.relname = {0} and a.attnum > 0  and a.attrelid = c.oid and a.atttypid = t.oid  ORDER BY a.attnum ",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}
			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				this.sql = MessageFormat.format(
						"select cast(a.name as varchar(50)) column_name,  cast(b.name as varchar(50)) data_type,  cast(e.value as varchar(200)) comment,  cast(ColumnProperty(a.object_id,a.Name,'''Precision''') as int) num_precision,  cast(ColumnProperty(a.object_id,a.Name,'''Scale''') as int) num_scale,  a.max_length,  (case when a.is_nullable=1 then '''y''' else '''n''' end) nullable   from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id left join sys.objects c on a.object_id=c.object_id and c.type='''U''' left join sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.column_id and e.class=1 where c.name={0}",
						new Object[] { TableConvert.getV(tableName.toLowerCase()) });
			}

			this.rs = this.stmt.executeQuery(this.sql);
			this.rs.last();
			int fieldNum = this.rs.getRow();
			if (fieldNum > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	private void formatFieldClassType(Columnt columnt) {
		String fieldType = columnt.getFieldType();
		String scale = columnt.getScale();
		columnt.setClassType("inputxt");
		if ("N".equals(columnt.getNullable())) {
			columnt.setOptionType("*");
		}
		if ("datetime".equals(fieldType) || fieldType.contains("time")) {
			columnt.setClassType("easyui-datetimebox");
		} else if ("date".equals(fieldType)) {
			columnt.setClassType("easyui-datebox");
		} else if (fieldType.contains("int")) {
			columnt.setOptionType("n");
		} else if ("number".equals(fieldType)) {
			if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
				columnt.setOptionType("d");
			}
		} else if ("float".equals(fieldType) || "double".equals(fieldType) || "decimal".equals(fieldType)) {
			columnt.setOptionType("d");
		} else if ("numeric".equals(fieldType)) {
			columnt.setOptionType("d");
		}
	}

	private String formatDataType(String dataType, String precision, String scale) {
		if (dataType.contains("char")) {
			dataType = "java.lang.String";
		} else if (dataType.contains("int")) {
			dataType = "java.lang.Integer";
		} else if (dataType.contains("float")) {
			dataType = "java.lang.Float";
		} else if (dataType.contains("double")) {
			dataType = "java.lang.Double";
		} else if (dataType.contains("number")) {
			if (StringUtils.isNotBlank(scale) && Integer.parseInt(scale) > 0) {
				dataType = "java.math.BigDecimal";
			} else if (StringUtils.isNotBlank(precision) && Integer.parseInt(precision) > 10) {
				dataType = "java.lang.Long";
			} else {
				dataType = "java.lang.Integer";
			}
		} else if (dataType.contains("decimal")) {
			dataType = "BigDecimal";
		} else if (dataType.contains("date")) {
			dataType = "java.util.Date";
		} else if (dataType.contains("time")) {
			dataType = "java.util.Date";
		} else if (dataType.contains("blob")) {
			dataType = "byte[]";
		} else if (dataType.contains("clob")) {
			dataType = "java.sql.Clob";
		} else if (dataType.contains("numeric")) {
			dataType = "BigDecimal";
		} else {
			dataType = "java.lang.Object";
		}
		return dataType;
	}

	public List readTableNamesByTableName(String tableNameLike) throws SQLException {
		List tableNames = new ArrayList(0);
		try {
			Class.forName(CodeResourceUtil.DIVER_NAME);
			this.conn = DriverManager.getConnection(CodeResourceUtil.URL, CodeResourceUtil.USERNAME,
					CodeResourceUtil.PASSWORD);
			this.stmt = this.conn.createStatement(1005, 1007);

			if (CodeResourceUtil.DATABASE_TYPE.equals("mysql")) {
				this.sql = MessageFormat.format(
						"select distinct table_name from information_schema.columns where table_schema = {0}",
						new Object[] { TableConvert.getV(CodeResourceUtil.DATABASE_NAME) }) + " and table_name like '"
						+ tableNameLike + "%'";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("oracle")) {
				this.sql = " select distinct colstable.table_name as  table_name from user_tab_cols colstable"
						+ " where colstable.table_name like '" + tableNameLike + "%'";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("postgresql")) {
				this.sql = "SELECT distinct c.relname AS  table_name FROM pg_class c" + " where c.relname like '"
						+ tableNameLike + "%'";
			}

			if (CodeResourceUtil.DATABASE_TYPE.equals("sqlserver")) {
				this.sql = "select distinct c.name as  table_name from sys.objects c " + " where c.name like '"
						+ tableNameLike + "%'";
			}

			this.rs = this.stmt.executeQuery(this.sql);
			while (this.rs.next()) {
				String tableName = this.rs.getString(1);
				tableNames.add(tableName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			closeConnection();
		} finally {
			closeConnection();
		}
		return tableNames;
	}
}
